//
//  MineViewController.m
//  WE
//
//  Created by 要帅气的生活 on 15/11/18.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "MineViewController.h"
#import "Head.h"
#import "SetViewController.h"
#import "LoginViewController.h"
#import "ConsultingViewController.h"
#import "MyDoctorViewController.h"
#import "MyApplicationControl.h"
#import "MyReservationControl.h"
#import "CaseHistoryControl.h"
#import "HelpAndFeedbackViewController.h"
#import "MBTwitterScroll.h"
#import "FocusonViewController.h"
#import "TheContactViewController.h"
#import "AuthenticateControl.h"
@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource,ABPeoplePickerNavigationControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong)MBTwitterScroll * tableView;
@property (nonatomic, strong)NSArray * array;
@property (nonatomic, strong)NSArray * arrayPng;
@end




@implementation MineViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = @[@[@"我的预约",@"我的申请",@"家庭病历"],@[@"我的咨询",@"我的医生"],@[@"帮助与反馈"],@[@"设置"]];
    self.arrayPng = @[[UIImage imageNamed:@"iconfont-xunyiyuyuexiangqing"],[UIImage imageNamed:@"iconfont-jia"],[UIImage imageNamed:@"iconfont-jiatingchengyuanguanli"],[UIImage imageNamed:@"iconfont-lianxizixun"],[UIImage imageNamed:@"iconfont-zhuanshujiatingyisheng"],[UIImage imageNamed:@"iconfont-lianxizixun"],[UIImage imageNamed:@"iconfont-bangzhu"],[UIImage imageNamed:@"iconfont-bangzhu"],[UIImage imageNamed:@"iconfont-bangzhu"],[UIImage imageNamed:@"iconfont-shezhi"]];
    //[self.view addSubview:self.tableView];
    
    [self addtableView];
    
    [self setExtraCellLineHidden:self.tableView.tableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //登陆成功后把用户名和密码存储到UserDefault
    NSString *name = [userDefaults objectForKey:@"name"];
    if (name == nil) {
        [self.navigationController pushViewController:[[LoginViewController alloc] init] animated:YES];
    }
}


- (void)addtableView
{
    self.tableView = [[MBTwitterScroll alloc] initTableViewWithBackgound:[UIImage imageNamed:@"Clouds"] avatarImage:[UIImage imageNamed:@"shouye4"] titleString:@"为了带给您更好的体验，请立即实名验证" subtitleString:@"" buttonTitle:@"家庭联系人"];
    _tableView.tableView.rowHeight = 45;
    _tableView.tableView.delegate = self;
    _tableView.tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 2;
    }else if (section == 2){
        return 1;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellOne";
    MineCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLbl.text = _array[indexPath.section][indexPath.row];
        cell.img.image = _arrayPng[indexPath.section *3 + indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 &&indexPath.row ==0) {
        [self.navigationController pushViewController:[[MyReservationControl alloc] init] animated:YES];
    }else if(indexPath.section == 0&&indexPath.row == 1){
        
//        ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
//        peoplePicker.peoplePickerDelegate = self;
//        [self presentViewController:peoplePicker animated:YES completion:nil];
        [self.navigationController pushViewController:[[MyApplicationControl alloc] init] animated:YES];
    }else if (indexPath.section == 0&&indexPath.row == 2){
        [self.navigationController pushViewController:[[CaseHistoryControl alloc] init] animated:YES];
    }else if (indexPath.section == 1&&indexPath.row == 0){
        ConsultingViewController *consulting = [ConsultingViewController new];
        consulting.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:consulting animated:YES];
        
        
    }else if (indexPath.section == 1&& indexPath.row == 1){
        MyDoctorViewController *mydoctor = [MyDoctorViewController new];
        mydoctor.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:mydoctor animated:YES];
    }else if(indexPath.section == 2 && indexPath.row == 0){
        [self.navigationController pushViewController:[[HelpAndFeedbackViewController alloc] init] animated:YES];
    }else if (indexPath.section == 3&& indexPath.row == 0){
        SetViewController *set = [[SetViewController alloc] init];
        set.view.backgroundColor = [UIColor whiteColor];
        [self.navigationController pushViewController:set animated:YES];
        
    }
    
    
    
}



- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}
/*
#pragma mark -- ABPeoplePickerNavigationControllerDelegate
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

{
    
    ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABMutableMultiValueRef address=ABRecordCopyValue(person, kABPersonAddressProperty);
    ABMutableMultiValueRef birthday=ABRecordCopyValue(person, kABPersonBirthdayProperty);
    ABMutableMultiValueRef creationDate=ABRecordCopyValue(person, kABPersonCreationDateProperty);
    ABMutableMultiValueRef date=ABRecordCopyValue(person, kABPersonDateProperty);
    ABMutableMultiValueRef department=ABRecordCopyValue(person, kABPersonDepartmentProperty);
    ABMutableMultiValueRef email=ABRecordCopyValue(person, kABPersonEmailProperty);
    //这部分需要真机才能运行，真机的时候打开
    ABMutableMultiValueRef firstNamePhonetic=ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);
    ABMutableMultiValueRef firstName=ABRecordCopyValue(person, kABPersonFirstNameProperty);
    ABMutableMultiValueRef instantMessage=ABRecordCopyValue(person, kABPersonInstantMessageProperty);
    ABMutableMultiValueRef jobTitle=ABRecordCopyValue(person, kABPersonJobTitleProperty);
    ABMutableMultiValueRef kind=ABRecordCopyValue(person, kABPersonKindProperty);
    ABMutableMultiValueRef lastNamePhonetic=ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);
    ABMutableMultiValueRef lastName=ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMutableMultiValueRef middleNamePhonetic=ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);
    ABMutableMultiValueRef middleName=ABRecordCopyValue(person, kABPersonMiddleNameProperty);
    ABMutableMultiValueRef modificationDate=ABRecordCopyValue(person, kABPersonModificationDateProperty);
    ABMutableMultiValueRef nickname=ABRecordCopyValue(person, kABPersonNicknameProperty);
    ABMutableMultiValueRef note=ABRecordCopyValue(person, kABPersonNoteProperty);
    ABMutableMultiValueRef organization=ABRecordCopyValue(person, kABPersonOrganizationProperty);
    ABMutableMultiValueRef phone=ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABMutableMultiValueRef prefix=ABRecordCopyValue(person, kABPersonPrefixProperty);
    ABMutableMultiValueRef relatedNames=ABRecordCopyValue(person, kABPersonRelatedNamesProperty);
    ABMutableMultiValueRef socialProfile=ABRecordCopyValue(person, kABPersonSocialProfileProperty);
    ABMutableMultiValueRef personSuffix=ABRecordCopyValue(person, kABPersonSuffixProperty);
    ABMutableMultiValueRef _URL=ABRecordCopyValue(person, kABPersonURLProperty);
    
    
    
    
    
    NSMutableArray *phones = [[NSMutableArray alloc] init];
    
    
    //    int i;
    
    for (int i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
        
        NSString *aPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, i);
        
        [phones addObject:aPhone];
        
    }
    
    NSLog(@"+qqqqqqqqqq++++%@",phones);
    
    
    NSLog(@"****************");
    NSString *mobileNo = [phones objectAtIndex:0];
    
    //    self.label.text = (NSString*)ABRecordCopyCompositeName(person);
    NSLog(@"++++++++++++++++++++%@",mobileNo);
    
    [self dismissModalViewControllerAnimated:YES];
    
    //[peoplePicker release];
    
    return NO;
    
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{
    
    
    [self dismissModalViewControllerAnimated:YES];
    
    //[peoplePicker release];
    
    return NO;
    
}



- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

{
    
    [self dismissModalViewControllerAnimated:YES];
    
    //[peoplePicker release];
    
}
*/


- (void) recievedMBTwitterScrollButtonClicked {
    NSLog(@"家庭联系人");
    TheContactViewController *the = [TheContactViewController new];
    the.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:the animated:YES];
}
-(void) ButtonClicked{
    NSLog(@"我的关注");
    FocusonViewController *focuson = [FocusonViewController new];
    focuson.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:focuson animated:YES];
    focuson.navigationController.navigationBar.hidden = NO;
}
-(void) genhuantupian{
    NSLog(@"更换头像");
    [self showUploadImg:nil];
}
-(void) RealNameAuthentication{
    [self.navigationController pushViewController:[[AuthenticateControl alloc] init] animated:YES];
    NSLog(@"实名验证");
}


-(void) recievedMBTwitterScrollEvent {
    //[self performSegueWithIdentifier:@"showPopover" sender:self];
}
- (void)showUploadImg:(NSString*)param
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction * CameraAction = [UIAlertAction actionWithTitle:@"相机" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType1 = UIImagePickerControllerSourceTypeCamera;
        picker.sourceType = sourceType1;
        
        [self presentViewController:picker animated:YES completion:nil];
    }];
    UIAlertAction * AlbumAction = [UIAlertAction actionWithTitle:@"相册" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType2 = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.sourceType = sourceType2;
        [self presentViewController:picker animated:YES completion:nil];
        
    }];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"h");
        
    }];
    [alertController addAction:CameraAction];
    [alertController addAction:AlbumAction];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
//相机相册的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSString * type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage * image = info[@"UIImagePickerControllerOriginalImage"];
        [picker dismissViewControllerAnimated:YES completion:nil];
        //        image = [ImageUtil reduce:image size:CGSizeMake(WindowWidth * 1.5, WindowHeight * 1.5) isAdjust:YES];
        
        
        BOOL upload = YES;
        //        if (!upload) {
        //            NSString * filePath = [ImageUtil saveImageLocal:UIImagePNGRepresentation(image)];
        //            [self uploadImageAfter:filePath];
        //            return;
        //        }
        //        if ([WebUtil isNet]) {
        [self uploadImageAfter:image];
        //
        //
        //            [[[WebUtil alloc] initWithUpload:upload_url _control:self] requestUpload:^(NSMutableDictionary * dic) {
        //
        //                [self uploadImageAfter:@""];
        //
        //            } _data:UIImagePNGRepresentation(image) _fileName:@"temp_image"];
        //        }
        //        
        
    }
    
    
}
- (void)uploadImageAfter:(id)object
{
    //NSLog(@"%@",object);
    
    if (object == nil) {
        
        
    }else{
        //添加图片
        _tableView.img.imgaeview.image = object;
      
    }

}
@end
